package com.woniu.order.service.impl;

import com.woniu.order.infra.model.CommentAttachment;
import com.woniu.order.infra.mapper.CommentAttachmentMapper;
import com.woniu.order.common.util.OssUtil;
import com.woniu.order.service.CommentAttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 评价附件表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class CommentAttachmentServiceImpl extends ServiceImpl<CommentAttachmentMapper, CommentAttachment> implements CommentAttachmentService {

    /**
     * 上传附件
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public String saveFile(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String type = fileName.substring(fileName.lastIndexOf("."));
        String name = System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("lxlg-attachment", name, file.getInputStream());
        //写入数据库
        CommentAttachment commentAttachment = new CommentAttachment();
        commentAttachment.setOriginalFileName(file.getOriginalFilename());
        commentAttachment.setCommentId(null);
        commentAttachment.setFileName(name);
        commentAttachment.setFileSize((int) file.getSize());
        String s=fileName.substring(fileName.lastIndexOf(".") + 1);
        commentAttachment.setFileSuffix(s);
        commentAttachment.setFilePath(path);
        commentAttachment.setUploadTime(new Date());
        save(commentAttachment);
        return path;
    }
}
